import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2025-04-14
 * Time: 15:15
 */
public class Demo33 {
    public static void main(String[] args) {
        Object locker = new Object();
        Thread t1 = new Thread(() -> {
            System.out.println("t1线程等待前");
            synchronized(locker) {//加锁-->加锁对象和wait()的对象要是同一个
                try {
                    locker.wait();//解锁-->等待notify的通知，唤醒
                    //加锁
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }//解锁
            System.out.println("t1线程等待后");
        });
        t1.start();
        Scanner sc = new Scanner(System.in);
        System.out.println("输入任意个数字");
        sc.nextInt();//保证t1线程先拿到锁
        //IllegalMonitorStateException-->虽然notify中不涉及加锁解锁的操作但是Java中强制要求notify要搭配synchronized
        synchronized (locker) {
            locker.notify();//通知--唤醒 notify()对象要和加锁以及wait()对象是同一个
        }
    }
    public static void main1(String[] args) {
        Object locker = new Object();
        //IllegalMonitorStateException--非法的锁状态（监视器--锁）
        Thread t1 = new Thread(() -> {
            System.out.println("t1线程等待前");
            try {
                locker.wait();//会进行解锁操作，但此时没有上锁，更没法解锁
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("t1线程等待后");
        });
        t1.start();

    }
}
